package com.paomi.client.activity.person.Adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.person.model.MyCommentBean;
import com.paomi.client.activity.reply.ReplyActivity;
import com.paomi.client.bean.UserUtils;
import com.paomi.client.utils.Datas;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyCommentListAdapter extends RecyclerView.Adapter<MyCommentListAdapter.ViewHolder> {
    public Typeface iconfont;

    private Activity mcontext;
    private List<MyCommentBean.Data> ticketList = new ArrayList<>();
    ClickView clickView;

    public void ClickView(ClickView clickView) {
        this.clickView = clickView;
    }

    public MyCommentListAdapter(Activity comtext) {
        mcontext = comtext;
        iconfont = Typeface.createFromAsset(mcontext.getAssets(), "iconfont.ttf");
    }

    public void setData(List<MyCommentBean.Data> jsonList) {
        ticketList = jsonList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.item_comment_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MyCommentBean.Data comment = ticketList.get(position);
        holder.my_comment.setVisibility(View.VISIBLE);
        holder.commentMsgNum.setText(comment.getReply() + "");
        holder.comment_delete.setVisibility(View.VISIBLE);
        holder.praiseNum.setText(comment.getLike() + "");
        holder.ll_show.setVisibility(View.VISIBLE);
        holder.comment_tip.setVisibility(View.GONE);
        GlideApp.with(mcontext).asBitmap().load(comment.getShowImg()).placeholder(R.mipmap.img_default_goods).into(holder.iv_show);
        holder.tv_showNam.setText(comment.getShowName());
        holder.commentContent.setText(comment.getContent());
        holder.commentTime.setText(Datas.getTimeElapse(comment.getTime()));

        if (UserUtils.getInstance().getUserInfo1() != null) {
            GlideApp.with(mcontext).asBitmap().load(UserUtils.getInstance().getUserInfo1()
                    .getHeadimgurl()).placeholder(R.mipmap.img_default_head).into(holder.userImage);
            holder.commentUser.setText(UserUtils.getInstance().getUserInfo1().getUserName());
        }
        //判断是否点赞
        if (comment.isLike()) {// true 已点赞 false 未点赞
            holder.praise_iv.setText(mcontext.getResources().getString(R.string.comment_praise));
            holder.praise_iv.setTextColor(mcontext.getResources().getColor(R.color.main_color));
        } else {
            holder.praise_iv.setText(mcontext.getResources().getString(R.string.comment_praise_un));
            holder.praise_iv.setTextColor(mcontext.getResources().getColor(R.color.text_gray_99));
        }
        holder.msg_num_iv.setTypeface(iconfont);
        holder.praise_iv.setTypeface(iconfont);
        holder.commentContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mcontext, ReplyActivity.class);
                intent.putExtra("getId", "" + comment.getTypeId());//商品id
                if (comment.getCommentType() == 0) {
                    intent.putExtra("pid", "" + comment.getId());//被评论的id
                } else {
                    intent.putExtra("pid", "" + comment.getMainCommentId());//被评论的id
                }
                mcontext.startActivity(intent);
            }
        });
        holder.ll_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickView != null) {
                    clickView.toShow(comment.getTypeId() + "");
                }
            }
        });

        holder.comment_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickView != null) {
                    clickView.delete(comment.getId() + "", position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return ticketList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.user_image)
        RoundedImageView userImage;
        @BindView(R.id.comment_user)
        TextView commentUser;
        @BindView(R.id.praise_num)
        TextView praiseNum;
        @BindView(R.id.comment_content)
        TextView commentContent;
        @BindView(R.id.comment_time)
        TextView commentTime;
        @BindView(R.id.comment_msg_num)
        TextView commentMsgNum;
        @BindView(R.id.ll_show)
        LinearLayout ll_show;
        @BindView(R.id.iv_show)
        ImageView iv_show;
        @BindView(R.id.tv_showNam)
        TextView tv_showNam;

        @BindView(R.id.msg_num_iv)
        TextView msg_num_iv;
        @BindView(R.id.praise_iv)
        TextView praise_iv;
        @BindView(R.id.comment_delete)
        TextView comment_delete;
        @BindView(R.id.comment_tip)
        TextView comment_tip;
        @BindView(R.id.my_comment)
        TextView my_comment;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ClickView {
        void delete(String id, int pos);

        void toShow(String goodId);
    }
}
